package practice1_100;

public class Practice14 {
    public String longestCommonPrefix(String[] strs){
        if(strs.length==0) return "";
        if(strs.length==1) return strs[0];
        String head=strs[0];
        int minIndex=head.length();
        int lenght=0;
        int maxIndex=0;//匹配的下标，初始化为0，表示strs中全都不匹配
        for (String s : strs){
            if(s.length()==0) {
                maxIndex=0;
                return "";
            }
            else {
                //取两次字符最短的长度
                lenght=strs[0].length()<s.length()?strs[0].length():s.length();
                for (int i=0;i<lenght;i++){
                    if(s.charAt(i)!=head.charAt(i)&&i==0) return "";

                    if (s.charAt(i)!=head.charAt(i)) {
                        maxIndex = i;
                        break;
                    }
                    else {
                        maxIndex = i+1;
                    }
                }
                minIndex=minIndex<=maxIndex?minIndex:maxIndex;
            }
        }
        return strs[0].substring(0,minIndex);

    }

//    public String longestCommonPrefix(String[] strs) {
//        if(strs.length==0) return "";
//        if(strs.length==1) return strs[0];
//        int len=strs.length;
//        int innerMinlen=strs[0].length();//strs中最短的长度
//        int minIndex=-1;//前缀的公共子串的最长下标
//        String a="";
//        for (int i=1;i<len;i++){
//            //以第一个为例
//            innerMinlen=innerMinlen<=strs[i].length()?innerMinlen:strs[i].length();
//            if(innerMinlen==0){
//                minIndex=0;
//                break;
//            }
//            a=strs[i];
//            for(int j=0;j<innerMinlen;j++){
//                if(strs[0].charAt(j)==a.charAt(j)) {
//                    minIndex=j+1;
//                }else {
//                    minIndex=j;
//                    break;
//                }
//            }
//        }
//       return strs[0].substring(0,minIndex);
//    }

    public static void main(String[] args) {
        String[] strs =new String[] {"c","acc","ccc"};
        String[] strs1 =new String[] {"flower","flow","flight"};
        String[] strs2 =new String[] {"abab","aba",""};
        String[] strs3 =new String[] {"a","a","b"};
        String[] strs4 =new String[] {"cir","car"};
        String[] strs5 =new String[] {"dog","racecar","car"};
        String[] strs6 =new String[] {"ab", "a"};
        String[] strs7 =new String[] {"aaa","aa","aaa"};



//        String bb=new  Practice14().longestCommonPrefix1(strs);
//        String cc=new  Practice14().longestCommonPrefix1(strs1);
Practice14 practice14=new Practice14();
        System.out.println(practice14.longestCommonPrefix(strs));
        System.out.println(practice14.longestCommonPrefix(strs1));
        System.out.println(practice14.longestCommonPrefix(strs2));
        System.out.println(practice14.longestCommonPrefix(strs3));
        System.out.println(practice14.longestCommonPrefix(strs4));
        System.out.println(practice14.longestCommonPrefix(strs5));
        System.out.println(practice14.longestCommonPrefix(strs6));
        System.out.println(practice14.longestCommonPrefix(strs7));
//        System.out.println(practice14.longestCommonPrefix1(strs5));


    }
}
